package com.atguigu.system.controller;


import com.atguigu.model.system.SysMenu;
import com.atguigu.model.vo.AssginMenuVo;
import com.atguigu.system.annotation.Log;
import com.atguigu.system.common.result.Result;
import com.atguigu.system.enums.BusinessType;
import com.atguigu.system.service.SysMenuService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 菜单表 前端控制器
 * </p>
 *
 * @author yqr
 * @since 2023-06-07
 */
@Api(tags = "菜单管理")
@RestController
@RequestMapping("/admin/system/sysMenu")
public class SysMenuController {
    @Autowired
    private SysMenuService sysMenuService;

    @Log(title = "菜单管理", businessType = BusinessType.SELECT)
    @PreAuthorize("hasAuthority('bnt.sysMenu.list')")
    @ApiOperation(value = "获取树型菜单")
    @GetMapping("findNodes")
    public Result findNodes(){
        List<SysMenu> menuList = sysMenuService.findNodes();
        return Result.ok(menuList);
    }

    @Log(title = "菜单管理", businessType = BusinessType.INSERT)
    @PreAuthorize("hasAuthority('bnt.sysMenu.add')")
    @ApiOperation("新增菜单")
    @PostMapping("save")
    public Result save(@RequestBody SysMenu sysMenu){
        sysMenuService.save(sysMenu);
        return Result.ok().message("新增菜单成功!");
    }

    @Log(title = "菜单管理", businessType = BusinessType.UPDATE)
    @PreAuthorize("hasAuthority('bnt.sysMenu.update')")
    @ApiOperation("修改菜单")
    @PutMapping("update")
    public Result update(@RequestBody SysMenu sysMenu){
        sysMenuService.updateById(sysMenu);
        return Result.ok().message("修改菜单成功");
    }

    @Log(title = "菜单管理", businessType = BusinessType.DELETE)
    @PreAuthorize("hasAuthority('bnt.sysMenu.remove')")
    @ApiOperation("删除菜单")
    @DeleteMapping("delete/{id}")
    public Result remove(@PathVariable String id){
        sysMenuService.removeMenuById(id);
        return Result.ok().message("删除菜单成功!");
    }

    @Log(title = "菜单管理", businessType = BusinessType.ASSGINAuth)
    @PreAuthorize("hasAuthority('bnt.sysRole.assignAuth')")
    @ApiOperation(value = "编辑角色的菜单权限")
    @GetMapping("/viewPermissions/{id}")
    public Result viewPermissions(@PathVariable String id){
        List<SysMenu> menuList = sysMenuService.distributionById(id);
        return Result.ok(menuList);
    }

    @Log(title = "菜单管理", businessType = BusinessType.ASSGINAuth)
    @PreAuthorize("hasAuthority('bnt.sysRole.assignAuth')")
    @ApiOperation(value = "修改角色的菜单权限")
    @PutMapping("/modifyPermissions")
    public Result modifyPermissions(@RequestBody AssginMenuVo assginMenuVo){
        sysMenuService.updatePermissions(assginMenuVo);
        return Result.ok().message("权限分配成功!");
    }

}

